const path = require('path');
const webpack = require('webpack');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const os = require("os");
const WORKING_DIR = process.cwd();

const getWLANIp = () => {
    let ipList = os.networkInterfaces();
    for (let key in ipList) {
        let ipItemList = ipList[key];
        for (let index = 0; index < ipItemList.length; index++) {
            let ipItem = ipItemList[index];
            if (ipItem.family === 'IPv6') continue;
            if (ipItem.address.indexOf('10.88.') === 0) {
                console.log(`webpack-dev-server running on ${ipItem.address}:9000`);
                return ipItem.address;
            }
        }
    }
    return;
};

module.exports = {
    entry: {
        app: [
            `webpack-dev-server/client?http://${getWLANIp() || 'localhost'}:9000/`,
            path.join(WORKING_DIR, "client/client.js")
        ]
    },
    devtool: 'inline-source-map',
    output: {
        path: path.join(__dirname, "static"),
        filename: "[name].[hash].js",
        publicPath: '/'
    },
    devServer: {
        contentBase: path.join(__dirname, "static"),
        publicPath: '/',
        inline: true,
        open: true,
        historyApiFallback: true,
        port: 9000,
        hot: true,
        host: getWLANIp(),
        noInfo: true,
        proxy: {
            "/api": {
                "target": getWLANIp()
            }
        },
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                query: {
                    presets: ['react', 'es2015', 'stage-0']
                }
            },
            {
                test: /\.html$/,
                use: [
                    {
                        loader: "html-loader",
                        options: {minimize: false}
                    }
                ]
            },
            {
                test: /(\.css)$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader'
                    },
                ]
            },
            {
                test: /\.less$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader'
                    },
                    {
                        loader: 'less-loader',
                        options: {
                            strictMath: true,
                            noIeCompat: true,
                            sourceMap: false,
                            javascriptEnabled: true
                        }
                    }
                ],
            },
            {
                test: /\.(png|jpg|gif|mp4)$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {}
                    }
                ]
            },
            {
                test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: "url-loader?limit=10000&mimetype=application/font-woff"
            },
            {
                test: /\.(ttf|eot|svg|otf)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: "file-loader"
            }
        ]
    },
    plugins: [
        new HtmlWebPackPlugin({
            title: '主页 - 大数据平台 - 惠生集团',
            template: path.join(WORKING_DIR, "client/index.html"),
            filename: "index.html"
        }),
        new webpack.NamedModulesPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.DefinePlugin({
            __isClient__: true
        }),
    ]
};